package meijuli.com.join51.business.utils;

import android.annotation.SuppressLint;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {
    public static final long ONE_MINUTE = 60000L;// 分钟毫秒
    public static final long ONE_HOUR = 3600000L;
    public static final long ONE_DAY = 86400000L;
    public static final long ONE_WEEK = 604800000L;
    public static final String FORMAT_TIME_MINUTES = "yyyy-MM-dd HH:mm"; // 精确到分钟
    public static final String FORMAT_TIME_HOURS_AND_MINUTES = "HH:mm"; // 只显示小时和分钟
    public static final String FORMAT_TIME_MINUTES_TWO = "yyyy-MM-dd HH:mm"; // 精确到分钟
    public static final String FORMAT_TIME_DAY = "yy-MM-dd"; // 精确到日
    public static final String FORMAT_TIME_MONTH_ADH_DAY = "MM月dd日"; // 精确到分钟
    public static final String FORMAT_TIME_ONLY_MONTH_DAY_MINUTE_SECOND = "MM月dd日 HH:mm"; // 只显示月、日、小时、分钟
    public static final String FORMAT_TIME_ONLY_HOUR_MINUTE_SECOND = "HH:mm:ss";
    private static final String ONE_SECOND_AGO = "秒前";
    private static final String ONE_MINUTE_AGO = "分钟前";
    private static final String ONE_HOUR_AGO = "小时前";
    private static final String ONE_DAY_AGO = "天前";
    private static final String ONE_MONTH_AGO = "月前";
    private static final String ONE_YEAR_AGO = "年前";



    /**
     * 时间戳格式化成日期
     *
     * @param time    时间戳
     * @param pattern 格式
     * @return
     */
    public static String getFormatTime(long time, String pattern) {
        Date date = new Date(time);
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    /**
     * 时间格式化成Calendar对象
     *
     * @param time
     * @return
     */
    public static Calendar getCalendar(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time));
        return calendar;
    }

    /**
     * 设置日期加减
     *
     * @param time  设置的时间 毫秒
     * @param field 要加减的字段
     * @param value 要加减的值
     */
    public static long setDateAddMinus(long time, int field, int value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time));
        calendar.add(field, value);
        return calendar.getTimeInMillis();
    }

    /**
     * 显示几分钟前、几小时前
     *
     * @param time
     * @return
     */
    public static String format(long time) {
        long delta = new Date().getTime() - time;
        if (delta < 1L * ONE_MINUTE) {
            long seconds = toSeconds(delta);
            return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO;
        }
        if (delta < 45L * ONE_MINUTE) {
            long minutes = toMinutes(delta);
            return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;
        }
        if (delta < 24L * ONE_HOUR) {
            long hours = toHours(delta);
            return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
        }
        if (delta < 48L * ONE_HOUR) {
            return "昨天";
        }
        return getFormatTime(time, "M月d");
    }

    private static long toSeconds(long date) {
        return date / 1000L;
    }

    private static long toMinutes(long date) {
        return toSeconds(date) / 60L;
    }

    private static long toHours(long date) {
        return toMinutes(date) / 60L;
    }

    private static long toDays(long date) {
        return toHours(date) / 24L;
    }

    private static long toMonths(long date) {
        return toDays(date) / 30L;
    }

    private static long toYears(long date) {
        return toMonths(date) / 365L;
    }

    /**
     * 格式化动态列表时间
     * 0 到 60 分钟内，展示 x 分钟前  （特殊：0 分钟就是 1 分钟前）
     * 1 到 24 小时内，展示 x 小时前
     * 24 小时后，展示 x月y日
     */
    public static String formatDynamicListTime(long time) {
        long currentTime = System.currentTimeMillis();
        long resultTime = currentTime - time;
        if (resultTime < ONE_MINUTE) {
            return "1分钟前";
        } else if (resultTime < 60 * ONE_MINUTE && resultTime >= ONE_MINUTE) {
            return resultTime / ONE_MINUTE + "分钟前";
        } else if (resultTime >= ONE_HOUR && resultTime <= ONE_DAY) {
            return resultTime / ONE_HOUR + "小时前";
        } else {
            return getFormatTime(time, FORMAT_TIME_MONTH_ADH_DAY);
        }
    }

    /**
     * 计算年龄
     *
     * @param birthdayTime 出生日期时间戳
     * @return
     */
    public static long getAge(long birthdayTime) {
        return ((System.currentTimeMillis() - birthdayTime) / 1000 / 60 / 60 / 24 / 365 + 1);
    }
}


